from appium import webdriver
from appium.options.android import UiAutomator2Options

from common.yamlUtil import YamlUtil


class DriverUtil:

    def connect_driver(self):
        conf = YamlUtil('config.yaml').read_yaml()
        des = {
            "automationName": f"{conf['automationName']}",
            "platformName": f"{conf['platformName']}",
            "platformVersion": f"{conf['platformVersion']}",
            "deviceName": f"{conf['deviceName']}",
            "appPackage": f"{conf['appPackage']}",
            "appActivity": f"{conf['appActivity']}",
            "unicodeKeyboard": conf['unicodeKeyboard'],
            "resetKeyboard": conf['resetKeyboard'],
            "noReset": conf['noReset'],
            "newCommandTimeout": conf['newCommandTimeout'],
            "autoGrantPermissions": conf['autoGrantPermissions']
        }

        try:
            driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', options= UiAutomator2Options().load_capabilities(des))
            print("已连接到手机")
            # print("手机型号为：")
            return driver
        except:
            print("未连接到手机")
            return None
